package com.hmeter.hadoop.hbase.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledChoice;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.hadoop.hbase.HBaseGetSampler;

public class HBaseGetSamplerGui extends AbstractSamplerGui {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -811183037837959144L;
	
	private JLabeledTextField zookeeperQuorumTextField = null;
	private JLabeledTextField hbaseTableTextField = null;
	private JLabeledTextField startRowKeyTextField = null;
	private JLabeledTextField stopRowKeyTextField = null;
	private JLabeledTextField numberOfRowsToRetrieveTextField = null;
	private JLabeledTextField firstColumnFamilyTextField = null;
	private JLabeledTextField firstColumnQualifierTextField = null;
	private JLabeledTextField secondColumnFamilyTextField = null;
	private JLabeledTextField hbaseFilterTextField = null;
	private JLabeledTextField delimiterTextField = null;
	
	private JLabeledChoice hbaseOperatorLabeledChoice = null;

	private JCheckBox stopRowKeyCheckBox = null;
	private JCheckBox numberOfRowsToRetrieveCheckBox = null;
	
	private JCheckBox firstCheckBox = null;
	private JCheckBox secondCheckBox = null;
	private JCheckBox thirdCheckBox = null;
	private JCheckBox fourthCheckBox = null;
	private JCheckBox fifthCheckBox = null;
	
	public HBaseGetSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
            
        zookeeperQuorumTextField = new JLabeledTextField("Zookeeper quorum");
        hbaseTableTextField = new JLabeledTextField("HBase table");
        startRowKeyTextField = new JLabeledTextField("Start row key");
        stopRowKeyTextField = new JLabeledTextField("");
        numberOfRowsToRetrieveTextField = new JLabeledTextField("");
        firstColumnFamilyTextField = new JLabeledTextField("Column family");
        firstColumnQualifierTextField = new JLabeledTextField("Column qualifier");
        secondColumnFamilyTextField = new JLabeledTextField("Column family");
        hbaseFilterTextField = new JLabeledTextField("HBase filter");
        delimiterTextField = new JLabeledTextField("Filter delimiter");
        
        stopRowKeyCheckBox = new JCheckBox("Use stop row key");
        numberOfRowsToRetrieveCheckBox = new JCheckBox("Use number of rows to retrieve");
        firstCheckBox = new JCheckBox("Get value by row key, family and qualifier");
        secondCheckBox = new JCheckBox("Get all values from column family by row key and family specified");
        thirdCheckBox = new JCheckBox("Get all content by row key ");
        fourthCheckBox = new JCheckBox("Get all content by row key from latest timestamp");
        fifthCheckBox = new JCheckBox("Get all content by row key and specified filters");
        
        hbaseOperatorLabeledChoice = new JLabeledChoice("Operator", new String[] { "MUST_PASS_ALL", "MUST_PASS_ONE" });
        
        ButtonGroup options = new ButtonGroup();
        ButtonGroup subOptions = new ButtonGroup();
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        
        inputPanel.add(zookeeperQuorumTextField);
        inputPanel.add(hbaseTableTextField);
        inputPanel.add(startRowKeyTextField);
        inputPanel.add(stopRowKeyCheckBox);
        inputPanel.add(stopRowKeyTextField);
        inputPanel.add(numberOfRowsToRetrieveCheckBox);
        inputPanel.add(numberOfRowsToRetrieveTextField);

        //first panel
        VerticalPanel firstPanel = new VerticalPanel();
        firstPanel.setBorder(BorderFactory.createTitledBorder("Get one value"));
        
        firstPanel.add(firstCheckBox);
        firstPanel.add(firstColumnFamilyTextField);
        firstPanel.add(firstColumnQualifierTextField);
        
        //second panel
        VerticalPanel secondPanel = new VerticalPanel();
        secondPanel.setBorder(BorderFactory.createTitledBorder("Get all column family values"));
        
        secondPanel.add(secondCheckBox);
        secondPanel.add(secondColumnFamilyTextField);
        
        //third panel
        VerticalPanel thirdPanel = new VerticalPanel();
        thirdPanel.setBorder(BorderFactory.createTitledBorder("Get all content"));
        
        thirdPanel.add(thirdCheckBox);
        
        //fourth panel
        VerticalPanel fourthPanel = new VerticalPanel();
        fourthPanel.setBorder(BorderFactory.createTitledBorder("Get all content from latest timestamp"));
        
        fourthPanel.add(fourthCheckBox);
        
        //fifth panel
        VerticalPanel fifthPanel = new VerticalPanel();
        fifthPanel.setBorder(BorderFactory.createTitledBorder("Get all content by filters"));
        
        fifthPanel.add(fifthCheckBox);
        fifthPanel.add(hbaseFilterTextField);
        fifthPanel.add(delimiterTextField);
        fifthPanel.add(hbaseOperatorLabeledChoice);
        
        options.add(firstCheckBox);
        options.add(secondCheckBox);
        options.add(thirdCheckBox);
        options.add(fourthCheckBox);
        options.add(fifthCheckBox);
        subOptions.add(stopRowKeyCheckBox);
        subOptions.add(numberOfRowsToRetrieveCheckBox);
        
        changePanelComponentsVisibility(firstCheckBox, firstColumnFamilyTextField, firstColumnQualifierTextField);
        changePanelComponentsVisibility(secondCheckBox, secondColumnFamilyTextField);
        changePanelComponentsVisibility(fifthCheckBox, hbaseFilterTextField, hbaseOperatorLabeledChoice, delimiterTextField);
        changePanelComponentsVisibility(stopRowKeyCheckBox, stopRowKeyTextField);
        changePanelComponentsVisibility(numberOfRowsToRetrieveCheckBox, numberOfRowsToRetrieveTextField);
        
        panel.add(inputPanel);
        panel.add(firstPanel);       
        panel.add(secondPanel);
        panel.add(thirdPanel);
        panel.add(fourthPanel);
        panel.add(fifthPanel);
        
        add(panel, BorderLayout.CENTER);
	}
	
	public void changePanelComponentsVisibility(JCheckBox checkBox, final Component...components)
	{		
		checkBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
				{
					for (Component c : components)
					{
						c.setEnabled(true);
					}
				}
				else if (e.getStateChange() == ItemEvent.DESELECTED)
				{
					for (Component c : components)
					{
						c.setEnabled(false);
					}
				}
			}
		}
		);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		zookeeperQuorumTextField.setText("");
		hbaseTableTextField.setText("");
		startRowKeyTextField.setText("");
		stopRowKeyTextField.setText("");
		numberOfRowsToRetrieveTextField.setText("");
		hbaseFilterTextField.setText("");
		firstColumnFamilyTextField.setText("");
		firstColumnQualifierTextField.setText("");
		secondColumnFamilyTextField.setText("");
		delimiterTextField.setText("");
		
		stopRowKeyCheckBox.setSelected(true);
		numberOfRowsToRetrieveCheckBox.setSelected(false);
		firstCheckBox.setSelected(true);
		secondCheckBox.setSelected(false);
		thirdCheckBox.setSelected(false);
		fourthCheckBox.setSelected(false);
		fifthCheckBox.setSelected(false);
		
		firstColumnFamilyTextField.setEnabled(true);
		firstColumnQualifierTextField.setEnabled(true);
		secondColumnFamilyTextField.setEnabled(false);
		numberOfRowsToRetrieveTextField.setEnabled(false);
		hbaseFilterTextField.setEnabled(false);
		delimiterTextField.setEnabled(false);
		
		hbaseOperatorLabeledChoice.setText("MUST_PASS_ALL");
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "HBase Get Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		HBaseGetSampler hbaseGetSampler = new HBaseGetSampler();
		modifyTestElement((TestElement) hbaseGetSampler);
		return (TestElement) hbaseGetSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof HBaseGetSampler)
		{
			HBaseGetSampler hbaseGetSampler = (HBaseGetSampler) element;
			hbaseGetSampler.setZookeeperQuorum(zookeeperQuorumTextField.getText());
			hbaseGetSampler.setHBaseTable(hbaseTableTextField.getText());
			hbaseGetSampler.setStartRowKey(startRowKeyTextField.getText());
			hbaseGetSampler.setStopRowKey(stopRowKeyTextField.getText());
			hbaseGetSampler.setNumberOfRowsToRetrieve(numberOfRowsToRetrieveTextField.getText());
			hbaseGetSampler.setHBaseFilter(hbaseFilterTextField.getText());
			hbaseGetSampler.setFirstColumnFamily(firstColumnFamilyTextField.getText());
			hbaseGetSampler.setFirstColumnQualifier(firstColumnQualifierTextField.getText());
			hbaseGetSampler.setSecondColumnFamily(secondColumnFamilyTextField.getText());
			hbaseGetSampler.setDelimiter(delimiterTextField.getText());
			hbaseGetSampler.setHBaseOperator(hbaseOperatorLabeledChoice.getText());
			hbaseGetSampler.setFirstCheckbox(firstCheckBox.isSelected());
			hbaseGetSampler.setSecondCheckbox(secondCheckBox.isSelected());
			hbaseGetSampler.setThirdCheckbox(thirdCheckBox.isSelected());
			hbaseGetSampler.setFourthCheckbox(fourthCheckBox.isSelected());
			hbaseGetSampler.setFifthCheckbox(fifthCheckBox.isSelected());
			hbaseGetSampler.setStopRowKeyBool(stopRowKeyCheckBox.isSelected());
			hbaseGetSampler.setNumberOfRowsToRetrieveBool(numberOfRowsToRetrieveCheckBox.isSelected());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof HBaseGetSampler)
		{
			HBaseGetSampler hbaseGetSampler = (HBaseGetSampler) element;
			zookeeperQuorumTextField.setText(hbaseGetSampler.getZookeeperQuorum());
			hbaseTableTextField.setText(hbaseGetSampler.getHBaseTable());
			startRowKeyTextField.setText(hbaseGetSampler.getStartRowKey());
			stopRowKeyTextField.setText(hbaseGetSampler.getStopRowKey());
			numberOfRowsToRetrieveTextField.setText(hbaseGetSampler.getNumberOfRowsToRetrieve());
			hbaseFilterTextField.setText(hbaseGetSampler.getHBaseFilter());
			firstColumnFamilyTextField.setText(hbaseGetSampler.getFirstColumnFamily());
			firstColumnQualifierTextField.setText(hbaseGetSampler.getFirstColumnQualifier());
			secondColumnFamilyTextField.setText(hbaseGetSampler.getSecondColumnFamily());
			delimiterTextField.setText(hbaseGetSampler.getDelimiter());	
			hbaseOperatorLabeledChoice.setText(hbaseGetSampler.getHBaseOperator());
			firstCheckBox.setSelected(hbaseGetSampler.isFirstCheckbox());
			secondCheckBox.setSelected(hbaseGetSampler.isSecondCheckbox());
			thirdCheckBox.setSelected(hbaseGetSampler.isThirdCheckbox());
			fourthCheckBox.setSelected(hbaseGetSampler.isFourthCheckbox());
			fifthCheckBox.setSelected(hbaseGetSampler.isFifthCheckbox());
			stopRowKeyCheckBox.setSelected(hbaseGetSampler.isStopRowKeyBool());
			numberOfRowsToRetrieveCheckBox.setSelected(hbaseGetSampler.isNumberOfRowsToRetrieveBool());
		}
	}
}
